/*
 * This packet is the response from the server to the client for the security
 * level setting request.
 *
 * @author Jonathan Walsh (jwalsh8484@gmail.com)
 */

package egs.packets;

import java.nio.ByteBuffer;

public class DisconnectPacket extends Packet {

    public enum ReasonCode {
        client_error ((short)1), 
            client_quit ((short)2),
            opp_error ((short)3),
            opp_timeout ((short)4),
            opp_left ((short)5);
        public final short value;
        ReasonCode( short value ) {
            this.value = value;
        };
    };
    
    private ReasonCode reason_code;
    
    public DisconnectPacket( ReasonCode reason_code ) {
        this.reason_code = reason_code;
        
        /*Set up the header*/
        message_type = MSG_DISCONNECT;
        game_name = 0;
        game_version = 0;
        data_area_size = 4; /*only two shorts in this packet*/
    }
    
    public DisconnectPacket( byte[] raw ) {
        ByteBuffer buf = setHeaderNew( raw );
        short val_from_packet = buf.getShort( HEADER_SIZE );
        reason_code = ReasonCode.class.getEnumConstants()[val_from_packet-1];
    }
    
    public byte[] getRaw() {
        ByteBuffer buf = makeBufferWithHeader( data_area_size );
        buf.putShort( reason_code.value );
        buf.putShort( (short)0 );
        
        return buf.array();
    }
    
    
    public String toString() {
        return super.toString() + "\nreason_code = " + reason_code.value;
    }
    
    public ReasonCode get_reason_code() { return reason_code; }
}
